package com.weibo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weibo.service.SeparateImpl;
import com.weibo.util.Page;

public class ChangePage extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*与之前差不多，只是这里会接收到传过来的页码决定提取哪些数据*/
		SeparateImpl impl = new SeparateImpl();
		Page cp = new Page();
		
		List<Object> list = impl.findAll();//获取全部数据
		
		int maxPage = cp.getMaxPage(list, 5);//获取最大页码		
		
		String temp = request.getParameter("page");	//获取请求页码	
		int nowPage = cp.getPage(list, 5, temp);//验证页码的有效性
		
		List<Object> initList = cp.getInitPage(list, nowPage, 5);//获取指定页码的数据
		
		request.setAttribute("nowPage", nowPage);//当前页
		request.setAttribute("maxPage", maxPage);//最大页
		request.setAttribute("initlist", initList);//数据
		
		request.getRequestDispatcher("/example.jsp").forward(request, response);
		
	}

}
